package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.DeviceFaultRecordService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.DeviceFaultRecordDto;
import com.huilian.iotbox.data.enums.CardStatusEnum;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @date 2021/1/9 11:33
 */
@RestController
@RequestMapping("/api/deviceFaultRecord")
public class DeviceFaultRecordController extends BaseController {
    @Autowired
    private UserService userService;
    @Resource
    private DeviceFaultRecordService deviceFaultRecordService;

    @PostMapping("/findDeviceFaultRecordList")
    public ServerResponse findDeviceFaultRecordList(@RequestBody DeviceFaultRecordDto deviceFaultRecordDto, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        deviceFaultRecordDto.setPageNum(Integer.parseInt(request.getParameter("pageNum")));
        deviceFaultRecordDto.setPageSize(Integer.parseInt(request.getParameter("pageSize")));

        if (!RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId()) && CardStatusEnum.CLOSE.getCode().equals(userInfo.getCardStatus())) {
            return ServerResponse.createByErrorMessage("您没有权限查看，请联系管理员开通权限");
        }
        deviceFaultRecordDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(deviceFaultRecordService.findDeviceFaultRecordList(deviceFaultRecordDto));
    }
    @PostMapping("/updateState")
    public ServerResponse updateState(@RequestBody DeviceFaultRecordDto deviceFaultRecordDto, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if (!RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId()) && CardStatusEnum.CLOSE.getCode().equals(userInfo.getCardStatus())) {
            return ServerResponse.createByErrorMessage("您没有权限查看，请联系管理员开通权限");
        }
        deviceFaultRecordDto.setUserInfo(userInfo);
        return deviceFaultRecordService.updateState(deviceFaultRecordDto);
    }
    @PostMapping("/statRecord")
    public ServerResponse statRecord(@RequestBody DeviceFaultRecordDto deviceFaultRecordDto, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if (!RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId()) && CardStatusEnum.CLOSE.getCode().equals(userInfo.getCardStatus())) {
            return ServerResponse.createByErrorMessage("您没有权限查看，请联系管理员开通权限");
        }
        deviceFaultRecordDto.setUserInfo(userInfo);
        return deviceFaultRecordService.statRecord(deviceFaultRecordDto);
    }
}
